package com.fagp.mew.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fagp.mew.cms.messages.PageRequest;
import com.fagp.mew.cms.messages.PageResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mew.common.config.RedisConstantValue;
import com.mew.common.dc.db.BulletinService;
import com.mew.common.dc.db.MaintenanceService;
import com.mew.common.dc.mapper.BulletinMapper;
import com.mew.common.entity.sys.Bulletin;
import com.mew.common.entity.sys.Maintenance;
import com.mew.common.util.StringUtils;
import com.mew.common.vo.web.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysWebService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private BulletinMapper bulletinMapper;
    @Autowired
    private BulletinService bulletinService;
    @Autowired
    private MaintenanceService maintenanceService;
//    @Autowired
//    private RegisterFeignService registerFeignService;
    /**
     * 维护白名单
     */
    public PageResponse whitelist(){
        List<HashMap<String, String>> list = redisTemplate.opsForHash().values(RedisConstantValue.ADMIN_WHITE_LIST).stream().map(r -> {
            return new HashMap<String, String>() {{
                put("gid",  r+"");
            }};
        }).collect(Collectors.toList());
        return PageResponse.builder().data(list).build();
    }

    public PageResponse addwhitelist(Long uid) {
        if (null== uid || uid<0){
            return new PageResponse("id错误");
        }
        redisTemplate.opsForHash().put(RedisConstantValue.ADMIN_WHITE_LIST,uid,uid);
        return new PageResponse();
    }

    public PageResponse delwhitelist(String gid) {
        if (StringUtils.isEmpty(gid)){
            return new PageResponse("id错误");
        }
        redisTemplate.opsForHash().delete(RedisConstantValue.ADMIN_WHITE_LIST,gid);
        return new PageResponse();
    }

    public PageResponse getMaintenances(PageRequest p){
        Page<UserVo> page = PageHelper.startPage(p.getPage(), p.getLimit());
//        List<Maintenance> bulletins = maintenanceMapper.selectList(new QueryWrapper<Maintenance>().orderByDesc("id"));
        List<Maintenance> bulletins = maintenanceService.findMaintenance();
        PageResponse pageResponse = new PageResponse();
        pageResponse.data =  bulletins;
        pageResponse.count = page.getTotal();
        return pageResponse;
    }

    public PageResponse addMaintenance(Maintenance m){
//        maintenanceMapper.insert(m);
//        registerFeignService.updateTmsOnlineALL(OnlineType.Unbound.value());
        maintenanceService.addMaintenance(m);
        return new PageResponse();
    }

    public PageResponse maintenanceState(Integer id,Integer state) {

//        Maintenance newb = new Maintenance();
//        newb.setId(id);
//        newb.setState(state==1?0:1);
//        maintenanceMapper.updateById(newb);
//
//        registerFeignService.updateTmsOnlineALL(newb.getState()==1?OnlineType.Unbound.value():OnlineType.Online.value());
        maintenanceService.updateMaintenance(id, state);
        return new PageResponse();
    }

    public PageResponse bulletin(PageRequest p) {
        Page<UserVo> page = PageHelper.startPage(p.getPage(), p.getLimit());
        List<Bulletin> bulletins = bulletinMapper.selectList(new QueryWrapper<Bulletin>().orderByDesc("id"));
        PageResponse pageResponse = new PageResponse();
        pageResponse.data =  bulletins;
        pageResponse.count = page.getTotal();
        return pageResponse;
    }

    public PageResponse upBulletin(Bulletin vo) {
        bulletinMapper.insert(vo);
        bulletinService.lodeBulletin();
        return new PageResponse();
    }

    public PageResponse bulletinState(Integer id,Integer state) {

        Bulletin newb = new Bulletin();
        newb.setId(id);
        newb.setState(state==1?0:1);
        bulletinMapper.updateById(newb);
        bulletinService.lodeBulletin();
        return new PageResponse();
    }
}
